package com.hdy.redislikerankinglist.service;

import com.hdy.redislikerankinglist.pojo.RestResponse;
import com.hdy.redislikerankinglist.utils.RedisUtil;
import com.hdy.redislikerankinglist.utils.ResultGenerator;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Set;

@Service
@RequiredArgsConstructor
public class RedisService {
    private final RedisUtil redisUtil;
    private final String success="200";
    private final String fail="500";

    public RestResponse giveLike(String id, String username) {

        boolean member = redisUtil.isMember("like:"+id, username);
        if (member) {
            Long aLong = redisUtil.sRem("like:" + id, username);
            return aLong!=1? ResultGenerator.genSuccessResult(fail,"Error!"):ResultGenerator.genSuccessResult(success,"取消点赞!");
        }else {
            redisUtil.sAdd("like:"+id,username);
            //redisUtil.zSScore("rank",id);
            return ResultGenerator.genSuccessResult(success,"点赞成功!");
        }
    }

    public RestResponse isGiveLike(String id, String username) {
        boolean member = redisUtil.isMember("like:"+id, username);
        if (member) {
            return ResultGenerator.genSuccessResult("已点赞");
        }
        return ResultGenerator.genSuccessResult("未点赞");
    }

    public RestResponse getGiveLikeList(String id) {
        Set<String> set = redisUtil.sMembers("like:"+id);
        return ResultGenerator.genSuccessResult(set);
    }

    public RestResponse getGiveLikeSize(String id) {
        Long aLong = redisUtil.size("like:"+id);
        return ResultGenerator.genSuccessResult(aLong);
    }
}
